import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router' // 内置router ts类型

import LocalCache from '@/utlis/cache'
// import { firstMenu } from '@/utlis/map-menus'
// import { mapMenusToRoutes } from '@/utlis/map-menus'
// import store from '@/store'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/index.vue')
    // chlidren:[] -> 根据userMenus
  }
  // {
  //   path: '/:pathMatch(.*)*',
  //   name: 'not-found',
  //   component: () => import('@/views/not-found/not-found.vue')
  // }
]

const router = createRouter({
  routes,
  history: createWebHashHistory()
})

// router.beforeEach(to => {
//   if (to.path !== '/login') {
//     const token = LocalCache.getCache('token')
//     if (!token) {
//       return '/login'
//     }
//   }

// mapMenusToRoutes
// const userMenus = (store.state as any).login.userMenus
// const routes = mapMenusToRoutes(userMenus)

// routes.forEach(route => {
//   router.addRoute('main', route)
// })
// if (to.path === '/main') {
//   return firstMenu.url
// }
// })

export default router
